﻿using Dapper;
using Domain.Entity.System_dictionary;
using Domain.IRepository.System_dictionary;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace EFCore.Repository.System_dictionary
{
    [Dependency(ServiceLifetime.Scoped)]
    public class Sys_Dict_dataRepoitory : ISys_dict_dataRepository
    {
        private readonly MyDbContext myDbContext;

        public Sys_Dict_dataRepoitory(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public List<Sys_dict_data> GetData_status(string type)
        {
            string sql = $"select * from [dbo].[Sys_dict_data] where Dict_type='{type}'";
            List<Sys_dict_data> list = myDbContext.Database.GetDbConnection().Query<Sys_dict_data>(sql).ToList();
            return list;

        }

        public List<Sys_dict_data> Getsys_Dict_Types(string Dict_name)
        {
            string sql = $@"select * from [dbo].[Sys_dict_type] where Dict_name='{Dict_name}'";
            Sys_dict_type? type = myDbContext.Database.GetDbConnection().Query<Sys_dict_type>(sql).FirstOrDefault();
            sql = $@"select * from [dbo].[Sys_dict_data] where Dict_type='{type.Dict_type}'";
            return myDbContext.Database.GetDbConnection().Query<Sys_dict_data>(sql).ToList();
        }

        public string GetSys_Dict_datacode(int Dict_code)
        {
            string sql = $"select * from [dbo].[Sys_dict_data] where Dict_code='{Dict_code}'";
            Sys_dict_data? sys_Dict_Data = myDbContext.Database.GetDbConnection().Query<Sys_dict_data>(sql).FirstOrDefault();
            return sys_Dict_Data.Dict_label;
        }
    }
}
